/**
 * Copyright 2011-2012 University of Illinois
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* ---------------------------------------------------------------- */
/*                  Initialize namespace properties                 */
/* ---------------------------------------------------------------- */

OAA_CI.media = OAA_CI.media || {};

OAA_CI.media.mediaView  = new OAA_CI.SidebarView('ID_TREECHILDREN_MEDIA');

OAA_CI.media.views = [OAA_CI.media.mediaView];

OAA_CI.media.hideMeter = function () { OAA_CI.media.meter.hidden = true; };
OAA_CI.media.showMeter = function () { OAA_CI.media.meter.hidden = false; };

OAA_CI.sidebar = OAA_CI.media;

/**
 * getDisplayProperties
 *
 * @desc Add getDisplayProperties method to cache elements;
 *       called by populateTreeAs... functions.
 *
 * @return List of property values to be displayed in the sidebar
 */


OpenAjax.a11y.cache.MediaElement.prototype.getDisplayProperties = function () {
  var de = this.dom_element;

  var cache_nls = OpenAjax.a11y.cache_nls;

  var is_video               = cache_nls.getValueNLS('is_video',              this.is_video);
  var is_audio               = cache_nls.getValueNLS('is_audio',              this.is_audio);
  var has_caption            = cache_nls.getValueNLS('has_caption',           this.has_caption);
  var has_audio_description  = cache_nls.getValueNLS('has_audio_description', this.has_audio_description);
  var has_text_alternative   = cache_nls.getValueNLS('has_text_alternative',  this.has_text_alternative);
  
  return [de.tag_name, is_audio, is_video, has_caption, has_audio_description, has_text_alternative, de.getAccessibility()];
};

/**
 * @function onLoad
 *
 * @memberOf OAA_CI.media
 *
 * @desc Initialize sidebar and populate its default tabpanel.
 */

OAA_CI.media.onLoad = function () {

  var config = {
   rules      : true,
   attributes : true,
   properties : true,
   style      : false,
   events     : true
  };

  var pt = new OAA_CI.Properties.PropertiesTabPanel("ID_PROPERTIES", config);
  OAA_CI.media.mediaView.addProperties(pt);
  
  OAA_CI.images_nls_strings = document.getElementById("ID_STRINGBUNDLE_IMAGES");
  OAA_CI.media_nls_strings = document.getElementById("ID_STRINGBUNDLE_MEDIA");
  OAA_CI.abbreviations_nls_strings = document.getElementById("ID_STRINGBUNDLE_ABBREVIATIONS");

  OAA_CI.media.meter = document.getElementById('ID_METER_EQUIVALENTS');
  OAA_CI.media.mediaView.isInitialized = false;
  OAA_CI.media.mediaView.showCacheItems();

};

/**
 * @function mediaView.showCacheItems
 *
 * @memberOf OAA_CI.media
 *
 * @desc Respond to the Media button in the sidebar's main tab panel.
 *       If the panel has not been initialized, populate it with media
 *       elements structured as a flat list. Update properties display
 *       and page highlighting based on selection state of panel.
 */

OAA_CI.media.mediaView.showCacheItems = function () {
  var view = OAA_CI.media.mediaView;

  if (!view.isInitialized) {
    // Save references
    view.tab = document.getElementById('ID_TAB_MEDIA');
    view.tree = document.getElementById('ID_TREE_MEDIA');
    view.cache = OAA_CI.ruleset.dom_cache.media_cache;

    // Populate the tabpanel
    view.cache.sortMediaElements('document_order', true);
    var media_elements = view.cache.media_elements;
    OAA_CI.populateTreeAsFlatList(media_elements, 'ID_TREECHILDREN_MEDIA');
    view.isInitialized = true;
  }

  view.updateItemSelectionDisplay();
};


/**
 * @function onItemSelectionChange
 *
 * @memberOf OAA_CI.media
 *
 * @desc Handle the onselect event from the XUL tree in the main tab panel
 *       by calling the updateItemSelectionDisplay method.
 *
 * @param {Number} index - Specifies the view that triggered the onselect event.
 */

OAA_CI.media.onItemSelectionChange = function (index) {

  var view = OAA_CI.media.views[index];
  view.updateItemSelectionDisplay();

};
